﻿using System;
using System.Threading.Tasks;

namespace Client.Panels
{
    public class ZhishuPanel : BasePanel
    {
        protected override string[] parentsName => new string[] { "BasePanel" };
        private DataCenter.Models.zhishuYL[] zs;
        //bool isFirstUpdate = true;

        public ZhishuPanel():base("市场一览")
        {
            FillFields<DataCenter.Models.zhishuYL>(panelName);
        }

        async Task<int> UpdatePanelAsync()
        {
            try
            {
                zs = await DataCenter.DataCenter.GetZhishuYL();
                UpdatePanel<DataCenter.Models.zhishuYL>(zs);
            }
            catch (DataCenter.MyException.APIReturnException e)
            {
                var l = e.Message.Split('~');
                if (l[0] == "1000")
                    Utility.ShowLoginAgain();
                throw;
            }
            return 0;
        }
        

        public override void OnAwake()
        {
            base.OnAwake();
            //YChanged += changedY;
            Managers.Manager.clientForm.dataGridView1.ContextMenuStrip = Managers.Manager.clientForm.stockContextMenuStrip;
            Managers.Manager.cronM.updateSuccessfullyAsync += UpdatePanelAsync;
            Managers.Manager.cronM.StartCron(true);
        }
        public override void OnSleep()
        {
            base.OnSleep();
            Managers.Manager.cronM.updateSuccessfullyAsync -= UpdatePanelAsync;
            Managers.Manager.hotIndex = null;
            Managers.Manager.clientForm.dataGridView1.ContextMenuStrip = null;
            //YChanged -= changedY;
        }
        public override void OnForeground()
        {
            base.OnForeground();
            Managers.Manager.clientForm.DisplayIndexPanel(false);
        }
        public override void OnBackground()
        {
            base.OnBackground();
            Managers.Manager.clientForm.DisplayIndexPanel();
        }
        //void changedY()
        //{
        //    if (y != -1)
        //        Managers.Manager.hotIndex = zs[y];
        //}

        protected override void ClearCron(){ }

        protected override void UpdateHotStockInfo()
        {
            if (y != -1)
                Managers.Manager.hotIndex = zs[y];
        }
    }
}
